package com.china08.yxyapi.api.home.activity.healthcompetition.tongjipage;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.huodong.healthcompetition.YxyCompetitionSchool;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huodong.YxyCompetitionSchoolRepository;

/**
 * 健康知识竞赛 活动排名(根据区域,学校名称查询;参与率 平均分排序)
 * 
 * @author zd.Jin
 *
 *
 */
@CrossOrigin
@RestController
public class HealthCompetionSchOrderPageController extends YxyApiBaseController {
	@Autowired
	private YxyCompetitionSchoolRepository yxyCompetitionSchoolRepository;

	@RequestMapping(value = "/api_yw/home/activity/healthcompetition/huodongorder/page", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
			@RequestBody ReqModel reqModel) {
		Pageable pageable = null;
		String schoolname = reqModel.schoolname;
		Integer order = reqModel.order;
		String areacode = reqModel.areacode;
		Page<YxyCompetitionSchool> findAll = null;
		if (order == 0) {
			pageable = reqModel.getPageable(Direction.ASC, "rate");
		} else if (order == 1) {
			pageable = reqModel.getPageable(Direction.DESC, "rate");
		} else if (order == 2) {
			pageable = reqModel.getPageable(Direction.ASC, "avg");
		} else if (order == 3) {
			pageable = reqModel.getPageable(Direction.DESC, "avg");
		}
		List<String> noList = new ArrayList<>();
		noList.add("ff8080815ba9422a015bb326acfa1599");
		noList.add("ff8080815ac67d12015b03a25cf70a5c");
		noList.add("ff8080815a032de4015a3c0fdf2d7a16");
		noList.add("ff808081598c885c01599b2857f52de7");
		noList.add("ff80808148f98a490148fcdab1760008");
		if (schoolname == null || "".equals(schoolname)) {// 当学校名称为空时
			if ("all".equals(areacode)) {// 当区域选择全部时
				findAll = yxyCompetitionSchoolRepository.findBySchoolIdNotInAndAreaCodeCity(noList, "230100", pageable);
			} else {// 当选择某个区县时
				findAll = yxyCompetitionSchoolRepository.findBySchoolIdNotInAndAreaCodeDistrict(noList, areacode,
						pageable);
			}
		} else {
			if ("all".equals(areacode)) {// 当区域选择全部时
				findAll = yxyCompetitionSchoolRepository.findBySchoolIdNotInAndAreaCodeCityAndSchoolNameLike(noList,
						"230100", schoolname.trim(), pageable);
			} else {// 当选择某个区县时
				findAll = yxyCompetitionSchoolRepository.findBySchoolIdNotInAndAreaCodeDistrictAndSchoolNameLike(noList,
						areacode, schoolname.trim(), pageable);
			}
		}
		RespModel respModel = null;
		List<RespModel> list = new ArrayList<RespModel>();
		for (YxyCompetitionSchool yxyCompetitionSchool : findAll) {
			respModel = new RespModel();
			respModel.name = yxyCompetitionSchool.getSchoolName();
			respModel.address = yxyCompetitionSchool.getAddress();
			respModel.total = yxyCompetitionSchool.getTotal();
			respModel.jointotal = yxyCompetitionSchool.getJoinTotal();
			respModel.rate = yxyCompetitionSchool.getRate();
			respModel.avg = yxyCompetitionSchool.getAvg();
			list.add(respModel);
		}
		Page<RespModel> page = new PageImpl<>(list, pageable, findAll.getTotalElements());
		return page;
	}

	protected static class ReqModel extends YxyApiPageReqModel {
		// 学校名称
		public String schoolname;
		// 区域id(全部传"all")
		public String areacode = "all";
		// 参与率排序(升序0,降序1)平均分排序(升序2,降序3)
		public Integer order = 1;

	}

	protected static class RespModel extends YxyApiRespModel {
		// 学校名称
		public String name;
		// 学校地址
		public String address;
		// 学生总数(小初高学生)
		public Integer total;
		// 参与人数
		public Integer jointotal;
		// 参与率
		public Double rate;
		// 平均分
		public Double avg;
	}
}
